/**
 * 
 */
package ocumed.persistenz.tests;

import java.util.Calendar;
import java.util.List;

import junit.framework.TestCase;
import ocumed.persistenz.dao.BenutzerDAO;
import ocumed.persistenz.dao.DAOFactory;
import ocumed.persistenz.dao.OrtDAO;
import ocumed.persistenz.dao.PatientDAO;
import ocumed.persistenz.hibernate.OcBenutzer;
import ocumed.persistenz.hibernate.OcOrt;
import ocumed.persistenz.hibernate.OcPatient;

/**
 * @author rudi
 *
 */
public class TestPatient extends TestCase {
    
    /**
     * test the list functionality for patient
     */
    public void testList() {
        PatientDAO pDAO = DAOFactory.get(DAOFactory.RDBMS).getPatientDAO();
        pDAO.start();
        
        System.out.println("show all: ");
        int i = 0;
        for (OcPatient ocPatient : pDAO.findAll()) {            
            System.out.println("=========");
            System.out.println(ocPatient.getPatientvorname() + " " + ocPatient.getPatientnachname());
            if (ocPatient.getOcOrt() != null) {
                System.out.println("ort: " + ocPatient.getOcOrt().getOrtname());
            }
            System.out.println(ocPatient.getPatientgebdatum().getTime());
            System.out.println("=========");
            i++;
        }
        
        assertEquals(i > 0, true);
    }
    
    /**
     * test filtered
     */
    public void testPatientFilter() {
        PatientDAO pDAO = DAOFactory.get(DAOFactory.RDBMS).getPatientDAO();
        pDAO.start();
        
        System.out.println("found matches...");
        for (OcPatient ocPatient : pDAO.findByName("29")) {
            System.out.println(ocPatient.getPatientvorname() + " " + ocPatient.getPatientnachname());
            System.out.println(ocPatient.getPatientgebdatum().getTime());
        }
        
    }
    
    /**
     * create a new one
     */
    public void testNewPatient() {
        OrtDAO oDAO = DAOFactory.get(DAOFactory.RDBMS).getOrtDAO();
        PatientDAO pDAO = DAOFactory.get(DAOFactory.RDBMS).getPatientDAO();
        BenutzerDAO bDAO = DAOFactory.get(DAOFactory.RDBMS).getBenutzerDAO();
        pDAO.start();
        oDAO.start();
        bDAO.start();
        
        List<OcOrt> oList = oDAO.findAll();
        List<OcBenutzer> bList = bDAO.findAll();
        
        OcPatient p = new OcPatient();
        p.setPatientvorname("from a junit");
        p.setPatientnachname("test");
        p.setOcOrt(oList.get(0));
        p.setPatientgebdatum(Calendar.getInstance());       
        p.setOcBenutzerByArztid(bList.get(0));
        p.setOcBenutzerByGeandertvonid(bList.get(0));
        p.setGeandertam(Calendar.getInstance());   
        
        Integer id = pDAO.persist(p);
        System.out.println("generated patient id: " + id);
        
        pDAO.commit();
    }

}
